import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class StructuredData {

	// Java data
    static ArrayList<DataWindow> allData = new ArrayList<DataWindow>();
	private static ClassifierBuilder with;
	
	public static ArrayList<DataWindow> getAllDataFrom(String filename, ClassifierBuilder _with) {
		File f = new File(filename);
		
		with = _with;
		
		try {
			processLineByLine(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return allData;
	}
	
	 /** Parse data file (CSV line by line). "timestamp,x-value,y-value,z-value"  */
	  public final static int processLineByLine(File fFile) throws FileNotFoundException {
		  int totalNbData = 0;
	    //Note that FileReader is used, not File, since File is not closeable
	    Scanner scanner = new Scanner(new FileReader(fFile));
	    try {
	      // Save Data in 256 samples window
	    	DataWindow window2Sample = new DataWindow(with);
	   	  
	      // use a Scanner to get each line
	    	// Ignore first line (CSV file)
	    	if (scanner.hasNextLine())
	    		scanner.nextLine();
	      while ( scanner.hasNextLine() ){
	        String line = scanner.nextLine();
	        String[] parser = line.split(",");
	        
	        // Process one line and save in a DataPoint
	        try {
		        DataPoint point = new DataPoint();
        		point.setTimestamp(Long.parseLong(parser[0]));
        		point.setX(Float.parseFloat(parser[1]));
        		point.setY(Float.parseFloat(parser[2]));
        		point.setZ(Float.parseFloat(parser[3]));
		        
		        // Finished parsing line, save the DataPoint in a window
		        // ========= /!\ =========
		        // CONSECUTIVE WINDOWS USED (COULD CHANGE TO 50% OVERLAPPING ONES??
		        // ========= /!\ =========
		        if (!window2Sample.hasSpace()) {
		        	// Full window, create a new one
		        	allData.add(window2Sample);
		        	window2Sample = new DataWindow(with);
		        }
	        	window2Sample.addDataPoint(point);
	        	totalNbData++;
		        
	        } catch (NumberFormatException e) {
	        	e.printStackTrace();
	        } catch (IndexOutOfBoundsException e) {
	        	e.printStackTrace();
	        }
	        
	      }
	      // Last DataWindow might not be full, but we must tell DataWindo class that it's finished (and fill the array with 0?)
	      window2Sample.complete();
	      // Or simply ignore the last points by commenting next line
	      allData.add(window2Sample);
	      
	      
	    }
	    finally {
	      //ensure the underlying stream is always closed
	      //this only has any effect if the item passed to the Scanner
	      //constructor implements Closeable (which it does in this case).
	      scanner.close();
	    }
	    
	    return totalNbData;
	  }
 }
